s = input().strip()
k = int(input())

def longest_substring(s, k):
    def helper(start, end):
        # 如果区间长度小于k，则返回0
        if end - start < k:
            return 0
        # 统计当前区间的字符频率
        count = {}
        for i in range(start, end):
            c = s[i]
            count[c] = count.get(c, 0) + 1
        # 寻找分割点
        for i in range(start, end):
            # 如果当前字符的频率小于k，则递归处理左右两部分
            if count[s[i]] < k:
                # 递归处理左右两部分
                left = helper(start, i)
                right = helper(i + 1, end)
                return max(left, right)
        # 当前区间所有字符都满足条件
        return end - start
    return helper(0, len(s))

print(longest_substring(s, k))
